<?php

class Common {

    
    //小区资产信息
    public static function assets($cmt_id,$batch_id) {


        $sql = [];
        $arg = [];

        //门脸个数
        $sql[] = 'select id,name,build_id,unit_id,build_name,unit_name from prop_front where cmt_id = ?';
        $arg[] = [$cmt_id];

        //仓库个数
        $sql[] = 'select id,name,build_id,unit_id,build_name,unit_name from prop_depot where cmt_id = ?';
        $arg[] = [$cmt_id];

        //车库个数
        $sql[] = 'select id,name,build_id,unit_id,build_name,unit_name from prop_garage where cmt_id = ?';
        $arg[] = [$cmt_id];


        //房屋个数
        $sql[] = 'select id,name,build_id,unit_id,build_name,unit_name,floor from prop_house where cmt_id = ?';
        $arg[] = [$cmt_id];

        //订单信息
        $sql[] = 'select * from fee_order_item_temp where cmt_id =? and batch_id = ?';
        $arg[] = [$cmt_id,$batch_id];

        $info = Mdb::query(MDB_MAIN,$sql,$arg);

        $orderObj = [];
        foreach($info[4] as $val){
            $assets = '';
            if($val['house_id'] != 0 ) $assets = 'house_' . $val['house_id'];
            if($val['front_id'] != 0 ) $assets = 'front_' . $val['front_id'];
            if($val['depot_id'] != 0 ) $assets = 'depot_' . $val['depot_id'];
            if($val['garage_id'] != 0 ) $assets = 'garage_' . $val['garage_id'];
            if($val['stall_id'] != 0 ) $assets = 'stall_' . $val['stall_id'];
            $orderObj[$assets] = $val['metric_now'];
        }

        $obj = [];
        $front = self::front($info[0],$orderObj);
        $depot = self::depot($info[1],$orderObj);
        $garage = self::garage($info[2],$orderObj);
        $house = self::house($info[3],$orderObj);

       foreach($house as &$bval){
           if(is_array($bval['children'])){
               foreach($bval['children'] as &$uval){
                   $bkey = 'b_' . $bval['value'] . '_' . $bval['label'];
                   $ukey = 'u_' . $uval['value'] . '_' . $uval['label'];
                   if(isset($front[$bkey][$ukey])){
                       $uval['children'][] = ['label'=>'门脸','value'=>'front','children'=>$front[$bkey][$ukey]];
                   }
                   
                   if(isset($depot[$bkey][$ukey])){
                    $uval['children'][] = ['label'=>'小屋','value'=>'depot','children'=>$depot[$bkey][$ukey]];
                    }
                    
                    if(isset($garage[$bkey][$ukey])){
                        $uval['children'][] = ['label'=>'车库','value'=>'garage','children'=>$garage[$bkey][$ukey]];
                    }
               }
           }
           if(isset($front[$bkey]['u_0_'])){
                $bval['children'][] =  ['label'=>'门脸','value'=>'front','children'=>$front[$bkey]['u_0_']];
            }

            if(isset($depot[$bkey]['u_0_'])){
                $bval['children'][] =  ['label'=>'小屋','value'=>'depot','children'=>$depot[$bkey]['u_0_']];
            }

            if(isset($garage[$bkey]['u_0_'])){
                $bval['children'][] =  ['label'=>'车库','value'=>'garage','children'=>$garage[$bkey]['u_0_']];
            }
       }
       if(isset($front['b_0_']['u_0_'])){
           $house[] = ['label'=>'门脸','value'=>'front','children'=>$front['b_0_']['u_0_']];
       }
       if(isset($depot['b_0_']['u_0_'])){
           $house[] = ['label'=>'小屋','value'=>'depot','children'=>$depot['b_0_']['u_0_']];
       }
       if(isset($garage['b_0_']['u_0_'])){
           $house[] = ['label'=>'车库','value'=>'garage','children'=>$garage['b_0_']['u_0_']];
       }


       return $house;
        
    }

    /**
     * 
     */
    public static function front($info,$orderObj){

        $obj = [];
        $bflag = 0;
        $uflag = 0;
        foreach($info as $val){
            $bkey = 'b_' . $val['build_id'] . '_' . $val['build_name'];
            $ukey = 'u_' . $val['unit_id'] . '_' . $val['unit_name'];
            if(!isset($obj[$bkey])) $obj[$bkey] = [];
            if(!isset($obj[$bkey][$ukey])) $obj[$bkey][$ukey] = [];
            $obj[$bkey][$ukey][] = ['value'=>$val['id'],'label'=>$val['name'],'assets'=>'front',"metric_now" => isset($orderObj['front_'.$val['id']]) ? $orderObj['front_'.$val['id']] : 0];
        }

        return $obj;
    }

     /**
     * 
     */
    public static function depot($info,$orderObj){
        $obj = [];
        foreach($info as $val){
            $bkey = 'b_' . $val['build_id'] . '_' . $val['build_name'];
            $ukey = 'u_' . $val['unit_id'] . '_' . $val['unit_name'];
            if(!isset($obj[$bkey])) $obj[$bkey] = [];
            if(!isset($obj[$bkey][$ukey])) $obj[$bkey][$ukey] = [];
            $obj[$bkey][$ukey][] = ['value'=>$val['id'],'label'=>$val['name'],'assets'=>'depot',"metric_now" => isset($orderObj['depot_'.$val['id']]) ? $orderObj['depot_'.$val['id']] : 0];
        }

        return $obj;
    }

     /**
     * 
     */
    public static function garage($info,$orderObj){
        $obj = [];
        foreach($info as $val){
            $bkey = 'b_' . $val['build_id'] . '_' . $val['build_name'];
            $ukey = 'u_' . $val['unit_id'] . '_' . $val['unit_name'];
            if(!isset($obj[$bkey])) $obj[$bkey] = [];
            if(!isset($obj[$bkey][$ukey])) $obj[$bkey][$ukey] = [];
            $obj[$bkey][$ukey][] = ['value'=>$val['id'],'label'=>$val['name'],'assets'=>'garage',"metric_now" => isset($orderObj['garage_'.$val['id']]) ? $orderObj['garage_'.$val['id']] : 0];
        }

        return $obj;
    }

     /**
     * 
     */
    public static function house($info,$orderObj){
        $obj = [];

        foreach($info as $val){
            $bkey = 'b_' . $val['build_id'] . '_' . $val['build_name'];
            $ukey = 'u_' . $val['unit_id'] . '_' . $val['unit_name'];
            $fkey = 'f_' . $val['floor'];

            if(!isset($obj[$bkey])) $obj[$bkey] = [];
            if(!isset($obj[$bkey][$ukey])) $obj[$bkey][$ukey] = [];
            if(!isset($obj[$bkey][$ukey][$fkey])) $obj[$bkey][$ukey][$fkey] = [];
            $obj[$bkey][$ukey][$fkey][] = ['value'=>$val['id'],'label'=>$val['name'],"assets"=>'house',"metric_now" => isset($orderObj['house_'.$val['id']]) ? $orderObj['house_'.$val['id']] : 0];
        }
        $list = [];
        foreach(array_keys($obj) as $bkey){
            $bdata = explode('_',$bkey); 
            $bformat = ['label' => $bdata[2],'value'=>$bdata[1],'children' => []];
            foreach(array_keys($obj[$bkey]) as $ukey){
                $udata = explode('_',$ukey); 
                $uformat = ['label' => $udata[2],'value'=>$udata[1],'children' => []];
                foreach(array_keys($obj[$bkey][$ukey]) as $floor){
                    $fdata = explode('_',$floor); 
                    $ffloor = ["label"=>$fdata[1] . "层",'value'=>$fdata[1],"children" =>$obj[$bkey][$ukey][$floor]];
                    $uformat['children'][] = $ffloor ;
                }
                $bformat['children'][] = $uformat;
            }
            $list[] = $bformat;
        }
        return $list;
    }

    /**
     * 用户和小区的关系
     */
    public static function community($user) {
        $user_id = $user['id'];
        $obj = [];

        $sql = [];
        $arg = [];

        //门脸信息
        $sql[] = 'select cmt_id,cmt_name from prop_front where user_id = ?';
        $arg[] = [$user_id];

        //房屋信息
        $sql[] = 'select cmt_id,cmt_name from prop_house where user_id = ?';
        $arg[] = [$user_id];

        $info = Mdb::query(MDB_MAIN,$sql,$arg);
        foreach($info as $val){
            foreach($val as $o){
                $obj[$o['cmt_id']] = $o['cmt_name'];
            }
        }

        $list = [];
        foreach(array_keys($obj) as $val){
            $list[] = ['id' => $val,"name" => $obj[$val]];
        }
        return $list;
    }

}